<script>
import { employmentType } from '@/constant'
import upload from '@/components/upload/upload.vue'

export default {
  name: 'employeesInfo',
  data() {
    return {
      employmentType,
      /* 表单数据对象 */
      formData: {
        mobile: '',
        username: '',
        workNumber: '',
        timeOfEntry: '',
        correctionTime: '',
        departmentName: '',
        staffPhoto: '',
        formOfEmployment: '',
        enableState: 0
      },
      /* 表单规则对象 */
      formRules: {}
    }
  },
  methods: {
    /* 获取用户信息的action */
    async fetchUserInfo() {
      try {
        this.loading = true
        const userInfo = await this.$store.dispatch('employeesStore/fetchUserInfo', this.$route.params.id)
        this.formData = userInfo
      } catch (error) {
        this.$message.error(error.message)
      } finally {
        this.loading = false
      }
    },
    /* 点击确定按钮的回调函数 */
    async handleSubmit() {
      try {
        const message = await this.$store.dispatch('employeesStore/updateEmployessInfo', this.formData)
        this.$message.success(message)
        this.$router.back()
      } catch (error) {
        this.$message.error(error.message)
      }
    }
  },
  mounted() {
    this.fetchUserInfo()
  },
  components: {
    upload
  }
}
</script>

<template>
  <div class="employeesInfo">
    <el-form label-width="100px">
      <el-form-item label="员工工号">
        <el-input placeholder="请输入员工工号" v-model="formData.workNumber"></el-input>
      </el-form-item>
      <el-form-item label="员工姓名">
        <el-input placeholder="请输入员工姓名" v-model="formData.username"></el-input>
      </el-form-item>
      <el-form-item label="员工手机">
        <el-input placeholder="请输入员工手机" v-model="formData.mobile"></el-input>
      </el-form-item>
      <el-form-item label="入职时间">
        <el-date-picker type="date" placeholder="请选择入职日期" v-model="formData.timeOfEntry"></el-date-picker>
      </el-form-item>
      <el-form-item label="转正时间">
        <el-date-picker type="date" placeholder="请选择转正日期" v-model="formData.correctionTime"></el-date-picker>
      </el-form-item>
      <el-form-item label="所属部门">
        <el-input placeholder="请输入所属部门" v-model="formData.departmentName"></el-input>
      </el-form-item>
      <el-form-item label="聘用形式">
        <el-select v-model="formData.formOfEmployment" placeholder="请选择聘用形式">
          <el-option v-for="(item,index) in employmentType" :value="item.id" :label="item.value" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="账户状态">
        <el-switch v-model.number="formData.enableState" :active-value="1" :inactive-value="0"></el-switch>
      </el-form-item>
      <el-form-item label="员工头像">
        <upload v-model="formData.staffPhoto"></upload>
      </el-form-item>
      <el-form-item>
        <el-button type="danger" @click="$router.back()">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style scoped lang="scss">
.el-form {
  width: 25%;

  .el-date-editor {
    width: 100%;
  }

  .el-select {
    width: 100%;
  }
}
</style>
